package com.base.web.auth.controller;

import com.base.web.auth.service.ResourceService;
import com.base.web.core.common.BaseConstant;
import com.base.web.core.common.JsonResult;
import com.base.web.core.util.UuidUtil;
import com.base.web.model.master.auth.MenuTree;
import com.base.web.model.master.auth.ResourceBean;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.TreeSet;

/**
 * Created with IntelliJ IDEA.
 * Description: 菜单控制类
 *
 * @Author: rentie
 * @Date: 2018/7/9 15:20
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    private Logger logger = LoggerFactory.getLogger(MenuController.class);

    @Autowired
    private ResourceService resourceService;

    @PostMapping("/add")
    public JsonResult save(ResourceBean bean) {
        Integer count;
        bean.setId(UuidUtil.getUUID32());
        try {
            count = resourceService.insertSelective(bean);
        } catch (Exception e) {
            logger.error("添加菜单异常={}", e.getMessage());
            return JsonResult.fail("添加菜单异常");
        }
        return JsonResult.success(count);
    }

    @PostMapping("/del")
    public JsonResult del(ResourceBean bean) {
        Integer count;
        try {
            bean.setStatus(BaseConstant.DATA_STATUS_DELETE);
            count = resourceService.updateByPrimaryKeySelective(bean);
        } catch (Exception e) {
            logger.error("删除菜单异常={}", e.getMessage());
            return JsonResult.fail("删除菜单异常");
        }
        return JsonResult.success(count);
    }

    @PostMapping("/statusChange")
    public JsonResult statusChange(ResourceBean bean) {
        Integer count;
        try {
            count = resourceService.updateByPrimaryKeySelective(bean);
        } catch (Exception e) {
            logger.error("启用或停用资源异常={}", e.getMessage());
            return JsonResult.fail("启用或停用资源异常");
        }
        return JsonResult.success(count);
    }
    @PostMapping("/update")
    public JsonResult update(ResourceBean bean) {
        Integer count;
        try {
            count = resourceService.updateByPrimaryKeySelective(bean);
        } catch (Exception e) {
            logger.error("修改菜单异常={}", e.getMessage());
            return JsonResult.fail("修改菜单异常");
        }
        return JsonResult.success(count);
    }

    /**
     * 查询菜单管理列表
     *
     * @param map page   当前页
     *            limit 每页个数
     * @return
     */
    @GetMapping("/manageList")
    public JsonResult list(@RequestParam Map<String, String> map) {
        PageInfo<ResourceBean> list = null;
        try {
            list = resourceService.queryManageList(map);
        } catch (Exception e) {
            logger.error("查询菜单列表异常={}", e.getMessage());
            return JsonResult.fail("查询菜单列表异常");
        }
        return JsonResult.success(list.getList(), list.getTotal());
    }

    /**
     * 查询菜单列表用于初始化下拉列表
     */
    @GetMapping("/menuSelect")
    public JsonResult listNoPage() {
        List<ResourceBean> list = null;
        try {
            list = resourceService.select(ResourceBean.defaultCondition());
        } catch (Exception e) {
            logger.error("查询菜单列表异常={}", e.getMessage());
            return JsonResult.fail("查询菜单列表异常");
        }
        return JsonResult.success(list);
    }

    /**
     * 获取用户菜单
     * @param id 用户id
     * @return
     */
    @GetMapping("/userMenu")
    public JsonResult getMenu(String id) {
        TreeSet<MenuTree> result = null;
        try {
            result = resourceService.queryUserMenuListById(id);
        } catch (Exception e) {
            logger.error("查询用户菜单异常={}", e.getMessage());
            return JsonResult.fail("查询用户菜单异常");
        }

        return JsonResult.success(result);
    }

    /**
     * 查询所有菜单用于生成树
     *
     * @return
     */
    @GetMapping("/menuTree")
    public JsonResult getAllMenu() {
        List<MenuTree> result = null;
        try {
            result = resourceService.queryMenuList("0");
        } catch (Exception e) {
            logger.error("查询所有菜单异常={}", e.getMessage());
            return JsonResult.fail("查询所有菜单异常");
        }

        return JsonResult.success(result);
    }


}